#include "key.h"
#include "config.h"
#include "wifi.h"
#define VOL_DOWN_SMARTCONFIG_PORT           GPIOA
#define VOL_DOWN_SMARTCONFIG_PIN            GPIO_PIN_10

#define VOL_UP_AP_PORT                      GPIOB
#define VOL_UP_AP_PIN                       GPIO_PIN_1

#define KEY_DOWN_FLAG         1
#define KEY_UP_FLAG           0

#define VOL_DOWN_FLAG_BIT     0
#define VOL_UP_FLAG_BIT       1

#define KEY_SHAKE_THR         20
#define KEY_ADD_VOL_THR      2000
#define KEY_DOWN_VOL_THR      2000
#define KEY_ADD_DEV_THR      5000
#define KEY_AP_SMART_THR      8000

#define NO_KEY_EVENT          0
#define VOL_DOWN_EVENT        1
#define VOL_UP_EVENT          2
#define SMARTCONFIG_EVENT     3
#define AP_EVENT              4
#define ADD_DEV_EVENT         5
#define CANCLE_ADD_DEV_EVENT  6


static uint8_t key_press_flag;
static uint8_t key_event;

static volatile uint32_t key_timer_cnt;
void key_timer_int_handler(void)
{
   if(key_press_flag)
   key_timer_cnt++;
}

void key_init(void)
{
   rcu_periph_clock_enable(RCU_GPIOA);
   rcu_periph_clock_enable(RCU_GPIOB);
   /* configure button pin as input */
   gpio_mode_set(VOL_DOWN_SMARTCONFIG_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, VOL_DOWN_SMARTCONFIG_PIN);
   gpio_mode_set(VOL_UP_AP_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, VOL_UP_AP_PIN); 
   
}

void key_mainloop(void)
{
   static uint8_t flag,on; 
   
   if(gpio_input_bit_get(VOL_DOWN_SMARTCONFIG_PORT, VOL_DOWN_SMARTCONFIG_PIN)==0)
   {
      if(key_press_flag>>VOL_UP_FLAG_BIT&0x1)
      {
         //vol up key was using
         return;
      }
      
      key_press_flag |= 1<<VOL_DOWN_FLAG_BIT;
      if((key_timer_cnt>=KEY_SHAKE_THR)&&(key_timer_cnt<KEY_DOWN_VOL_THR))
      {
         key_event = VOL_DOWN_EVENT;
         if((flag&(1<<VOL_DOWN_EVENT))==0)
         {
            gd_printf(("vol down sound prompt\r\n"));
            flag |= 1<<VOL_DOWN_EVENT;
         }
         
      }
      else if((key_timer_cnt>=KEY_DOWN_VOL_THR)&&(key_timer_cnt<KEY_ADD_DEV_THR))
      {
         key_event = ADD_DEV_EVENT;
         
         if((flag&(1<<ADD_DEV_EVENT))==0)
         {
            gd_printf(("add sub device sound prompt\r\n"));
            flag |= 1<<ADD_DEV_EVENT;
         }
      }
      else if((key_timer_cnt>=KEY_ADD_DEV_THR)&&(key_timer_cnt<KEY_AP_SMART_THR))
      {
         key_event = SMARTCONFIG_EVENT;
         
         if((flag&(1<<SMARTCONFIG_EVENT))==0)
         {
            gd_printf(("smartconfig sound prompt\r\n"));
            flag |= 1<<SMARTCONFIG_EVENT;
         }
      }
      
   }
   else if(gpio_input_bit_get(VOL_UP_AP_PORT, VOL_UP_AP_PIN)==0)
   {
      if(key_press_flag>>VOL_DOWN_FLAG_BIT&0x1)
      {
         //vol down key was using
         return;
      }
      key_press_flag |= 1<<VOL_UP_FLAG_BIT;
    

      if((key_timer_cnt>=KEY_SHAKE_THR)&&(key_timer_cnt<KEY_ADD_VOL_THR))
      {
         key_event = VOL_UP_EVENT;
         
         if((flag&(1<<VOL_UP_EVENT))==0)
         {
            gd_printf(("vol up sound prompt\r\n"));
            flag |= 1<<VOL_UP_EVENT;
         }
      }
      else if((key_timer_cnt>=KEY_ADD_VOL_THR)&&(key_timer_cnt<KEY_ADD_DEV_THR))
      {
         key_event = ADD_DEV_EVENT;
         if((flag&(1<<ADD_DEV_EVENT))==0)
         {
            gd_printf(("add sub device sound prompt\r\n"));
            flag |= 1<<ADD_DEV_EVENT;
         }
         
      }
      else if((key_timer_cnt>=KEY_ADD_DEV_THR)&&(key_timer_cnt<KEY_AP_SMART_THR))
      {
         key_event = AP_EVENT;
         
         if((flag&(1<<AP_EVENT))==0)
         {
            gd_printf(("ap sound prompt\r\n"));
            flag |= 1<<AP_EVENT;
         }
      }
   }
   else
   {
      switch(key_event)
      {
         case NO_KEY_EVENT:
              
         break;
         case VOL_DOWN_EVENT:
              
              gd_printf(("vol down effect\r\n"));
              rtc_show_time();
              //reset_factory_setting();
         break;
         case VOL_UP_EVENT:
              gd_printf(("vol up effect\r\n"));
         break;
         case SMARTCONFIG_EVENT:
              mcu_set_wifi_mode(SMART_CONFIG);
              gd_printf(("go in smartconfig effect\r\n"));
         break;
         case AP_EVENT:
              mcu_set_wifi_mode(AP_CONFIG);
              gd_printf(("go in ap effect\r\n"));
         break;
         case ADD_DEV_EVENT:
              gd_printf(("add sub device effect\r\n"));
              local_add_subdev_limit(1, 0, 60);
         break;
         case CANCLE_ADD_DEV_EVENT:
              gd_printf(("cancle add sub device effect\r\n"));
         break;
      }
      key_press_flag = 0;
      key_timer_cnt = 0;
      key_event = NO_KEY_EVENT;
      flag = 0;
   }
}
